package com.abe.bathsystem.contract.presenter;


import com.abe.bathsystem.contract.CommonContract;
import com.abe.bathsystem.contract.CommonModel;
import com.abe.bathsystem.entity.ReqBaseEntity;
import com.abe.bathsystem.entity.ReqBaseNoDataEntity;
import com.abe.bathsystem.entity.ReqConstant;
import com.abe.bathsystem.entity.http.HttpRoomEntity;
import com.abe.bathsystem.utils.NormalUtils;
import com.abe.libcore.utils.normal.ToastUtils;
import com.abe.mvpcore.BasePresenter;
import com.abe.mvpcore.OnResultListener;

import net.yet.campusdev.R;

public class ServerPresenter extends BasePresenter<CommonContract.ServerView, CommonModel> implements CommonContract.ServerPresenter {

    @Override
    public void getServerData() {
        getModel().getRoomData(new OnResultListener<ReqBaseEntity<HttpRoomEntity>>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onEnd() {
                ServerPresenter.this.onEnd(ServerPresenter.class.getSimpleName() + "-getRoomData");
            }

            @Override
            public void onSuccess(ReqBaseEntity<HttpRoomEntity> data) {
                if (ServerPresenter.this.onSuccess(data)) {
                    if (data.getCode() == ReqConstant.NET_RESULT_OK) {
                        getView().onServerData(data.getData());
                    } else {
                        String message = data.getMsg();
                        message = message + "!" + NormalUtils.getString(R.string.need_reset);
                        ToastUtils.show(message);
                        getView().onRoomUnregistered();
                    }
                }
            }

            @Override
            public void onFailure(String message) {
                ServerPresenter.this.onFailure(ServerPresenter.class.getSimpleName() + "-getRoomData");
            }
        });
    }

    @Override
    public void uploadClientId(String id) {
        getModel().uploadClientId(id, new OnResultListener<ReqBaseNoDataEntity>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onEnd() {
                ServerPresenter.this.onEnd(ServerPresenter.class.getSimpleName() + "-uploadClientId");
            }

            @Override
            public void onSuccess(ReqBaseNoDataEntity data) {
                if (ServerPresenter.this.onSuccess(data)) {
                    getView().onUploadClientId(data.getCode() == ReqConstant.NET_RESULT_OK);
                }
            }

            @Override
            public void onFailure(String message) {
                ServerPresenter.this.onFailure(ServerPresenter.class.getSimpleName() + "-uploadClientId");

            }
        });
    }
}
